//
//  UIViewExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/20.
//

import UIKit
import Toast_Swift

class UIViewExtension: NSObject {

}

extension UIView {
    
    /// 添加子view
    /// - Parameter childView: 子view
    func addChildView(_ childView: UIView) {
        childView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(childView)
    }
    
    func makeBorder(_ color: UIColor = .white, _ width: CGFloat = 1 ) {
        self.layer.borderColor = color.cgColor
        self.layer.borderWidth = width
        self.layer.masksToBounds = true
    }
    
    /// 画圆角
    func makeCorner(_ radius: CGFloat) {
        self.clipsToBounds = true
        self.layer.cornerRadius = radius
    }
    
    /// 将UIView转换成UIImage，ps:UIView尺寸要明确
    /// - Returns: 这个方法，在富文本中使用时，会有黑色背景 需要进一步确认
    func convertToUIImage() -> UIImage {
        self.layoutIfNeeded()
        var image = UIImage()
//        UIGraphicsBeginImageContextWithOptions(区域大小, 是否是非透明的, 屏幕密度)
        UIGraphicsBeginImageContextWithOptions(self.frame.size, true, UIScreen.main.scale)
        self.layer.render(in: UIGraphicsGetCurrentContext()!)
        self.layer.backgroundColor = UIColor.clear.cgColor
        image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
        UIGraphicsEndImageContext()
        return image
    }
    
    /// 画四角弧度
    /// - Parameters:
    ///   - roundingCorners: 弧角位置
    ///   - cornerRadii: 弧角数值
    func makeBezierPathCorner(_ roundingCorners: UIRectCorner,_ cornerRadii: CGFloat) {
        self.layoutIfNeeded()
        let maskPath = UIBezierPath.init(roundedRect: CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height), byRoundingCorners: roundingCorners, cornerRadii: CGSize.init(width: cornerRadii, height: cornerRadii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
    
    
    /// 给view底部添加一条线，默认与view宽度相等
    func addBottomLine(_ color: UIColor = color("#EDEEF1")) {
        
        let line = UIView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(0.5)
        }
        line.backgroundColor = color
    }
    
    /// 给view底部添加一条线，默认与view宽度相等
    func addCustomBottomLine(_ color: UIColor = color("#EDEEF1"), with left: CGFloat = 0,_ right: CGFloat = 0) {
        
        let line = UIView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.bottom.equalTo(self)
            make.left.equalTo(self).offset(left)
            make.right.equalTo(self).offset(-right)
            make.height.equalTo(0.5)
        }
        line.backgroundColor = color
    }
    
    /// 给view顶部添加一个与view大小相同的线
    func addTopLine(_ color: UIColor = color("#E9E9E9")) {
        let line = UIView()
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.height.equalTo(0.5)
        }
        line.backgroundColor = color
    }
    
    func toast(message: String) {
        if message.count == 0 {
           self.makeToast("系统异常", point: CGPoint.init(x: screenWidth/2.0, y: screenHeight/2.0 + 100), title: nil, image: nil, completion: nil)
            return
        }
        self.makeToast(message, point: CGPoint.init(x: screenWidth/2.0, y: screenHeight/2.0 + 100), title: nil, image: nil, completion: nil)
    }
    
    
    
    /// 指定位置大小的渐变色
    /// - Parameters:
    ///   - colors: 渐变色组
    ///   - frame: 渐变位置大小
    func addGradient(with colors: [CGColor], frame: CGRect) {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = frame
        gradientLayer.colors = colors
        layer.addSublayer(gradientLayer)
    }

    
}



